﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.IO;
using System.Data;
using System.Data.SqlClient;

namespace Koonsoft.Tool.ReviewBOSSLog
{
    public class BOSSFileHelper : ILogParser
    {
        private string importDir;
        private string exportDir;
        private string formatfile;
        private string sql;

        private SqlConnection sqlconn;
        private SqlCommand sqlcmd;

        public void TransformFileFormat()
        {
            importDir = ConfigurationManager.AppSettings["BOSSLogDir_UTF8"];
            exportDir = ConfigurationManager.AppSettings["BOSSLogDir_ANSI"];

            if (!System.IO.Directory.Exists(importDir))
            {
                throw new Exception("UTF8目录不存在。");
            }

            if (!System.IO.Directory.Exists(exportDir))
            {
                throw new Exception("ANSI目录不存在。");
            }

            DirectoryInfo dir = new DirectoryInfo(importDir);

            //TruncateTable();

            foreach (FileInfo file in dir.GetFiles())
            {
                Console.WriteLine(file.Name);

                StreamReader sr = new StreamReader(importDir + file.Name, Encoding.UTF8);
                string strBody = sr.ReadToEnd();
                sr.Close();

                StreamWriter sw = new StreamWriter(exportDir + file.Name, false, Encoding.Default);
                sw.Write(strBody);
                sw.Close();

                ImportDataIntoDatabase(exportDir + file.Name);
            }
        }

        public void ImportDataIntoDatabase(string filepath)
        {
            formatfile = ConfigurationManager.AppSettings["BOSSLogFormatFile"];

            sql = @"BULK insert operationLog from '" + filepath + "' with (FORMATFILE = '" + formatfile + "')";

            sqlconn = new SqlConnection();
            sqlconn.ConnectionString = ConfigurationManager.ConnectionStrings["LocalServer"].ConnectionString;

            sqlcmd = new SqlCommand();
            sqlcmd.CommandText = sql;
            sqlcmd.Connection = sqlconn;

            //sqlcmd.Parameters.Add("@filename", SqlDbType.VarChar, 256);
            //sqlcmd.Parameters["@filename"].Value = filepath;

            //sqlcmd.Parameters.Add("@formatfile", SqlDbType.VarChar, 256);
            //sqlcmd.Parameters["@formatfile"].Value = formatfile;

            try
            {
                sqlcmd.Connection.Open();
                sqlcmd.ExecuteNonQuery();
            }
            finally
            {
                sqlcmd.Connection.Close();
            }

        }

        public void TruncateTable()
        {
            sql = @"truncate table operationLog";

            sqlconn = new SqlConnection();
            sqlconn.ConnectionString = ConfigurationManager.ConnectionStrings["LocalServer"].ConnectionString;

            sqlcmd = new SqlCommand();
            sqlcmd.Connection = sqlconn;
            sqlcmd.CommandText = sql;

            try
            {
                sqlcmd.Connection.Open();
                sqlcmd.ExecuteNonQuery();
            }
            finally
            {
                sqlcmd.Connection.Close();
            }
        }
    }
}
